package com.dstation.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dstation.domain.OrderInfo;
import com.dstation.domain.UserInfo;
import com.dstation.service.interf.IOrderService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import javax.websocket.server.PathParam;
import java.math.BigDecimal;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

@Slf4j
@Controller
@RequestMapping(value = "/order")
public class OrderController {

    final int pageSize=10;

    @Autowired
    private IOrderService orderService;


    @RequestMapping(value = "myOrder")
    public String  queryMyOrder(@PathParam("page")Integer page, ModelMap modelMap, HttpSession httpSession){

        UserInfo userInfo=(UserInfo)httpSession.getAttribute("userInfo");


        //对合法性校验
        if(Objects.isNull(userInfo)||Objects.isNull(page)){
            return "user/loginAndRegister";
        }


        IPage<OrderInfo> list= orderService.findMyOrderById(new Page<OrderInfo>(page,pageSize),userInfo.getUserId());

        modelMap.put("orderInfoPage",list);


        return "main/myOrder";

    }


    @RequestMapping(value = "viewOrder")
    public String  queryMyOrderById(@PathParam("orderId")Integer orderId, ModelMap modelMap, HttpSession httpSession){



        IPage<OrderInfo> list= orderService.findOrderById(new Page<OrderInfo>(1,pageSize),orderId);

        modelMap.put("orderInfoPage",list);


        return "main/myOrder";

    }



    @ResponseBody
    @RequestMapping(value = "cancelOrder")
    public JSONObject cancelOrderIsLimit(@PathParam("orderId")Long orderId){
        JSONObject jsonObject=new JSONObject();

        boolean isCancel=false;
        boolean isTimeLimit=queryOrderIsTimeLimit(orderId);

        //订单已经逾期
        if (isTimeLimit){
            isCancel=false;
        }else{
            //防止订单状态异常导致错误，采用加锁
            Lock lock=new ReentrantLock();
            lock.lock();
            try {
                isCancel=orderService.dropOrderById(orderId);

            }catch (Exception e){
                e.printStackTrace();
            }finally {
                lock.unlock();
            }

        }

        jsonObject.put("isCancel",isCancel);

        return jsonObject;
    }

    @RequestMapping("showOrder")
    public String queryRecentOrder(@PathParam("page")Integer page, ModelMap modelMap){
        if(Objects.isNull(page)){
            return "";
        }

        IPage<OrderInfo> pageData=orderService.findAllOrder(new Page(page,pageSize));

        modelMap.put("pageData",pageData);

        return "admin/order";

    }


    @RequestMapping("payOrder")
    public String payOrderById(@Param("total")BigDecimal total,ModelMap modelMap){

        modelMap.put("total",total);
        return "main/payOrder";
    }


    private boolean queryOrderIsTimeLimit(long orderId) {
        return orderService.findOrderIsTimeLimit(orderId);
    }

}
